package com.younion.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.util.json.JsonMapper;
import com.younion.vo.RegisterUserVO;

/**
 * @author wells.wong
 * @description JsonUtils工具类：采用Gson，google的一个开源项目,实现json对象的解析以及json对象跟java对象的互转
 */
public class JsonUtils {

	public static final String EMPTY_ARRAY = "[]";
	public static final String EMPTY_COLLECTION = "{}";
	public static final String EMPTY_STRING = "";

	/**
	 * 将json对象转换成java对象
	 * 
	 * @param obj
	 * @return
	 * @throws JSONException
	 */
	public static String getJson2String(String jsonData, String key)
			throws JSONException {

		if (null == key)
			return null;

		JSONObject obj = new JSONObject(jsonData);
		String value = obj.get(key).toString();
		return value;
	}

	/**
	 * 将java对象转换成json对象
	 * 
	 * @param obj
	 * @return
	 */
	public static String parseObj2Json(Object obj) {

		if (null == obj)
			return null;
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder
				.setDateFormat(DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
		Gson gson = gsonBuilder.create();
		String objstr = gson.toJson(obj);
		return objstr;
	}

	/**
	 * 将java对象转换成json对象
	 * 
	 * @param obj
	 * @param regStr
	 *            需要显示的属性 多个属性用|号分隔
	 * @return
	 */
	public static String parseObj2Json(Object obj, final String regStr) {

		if (null == obj)
			return null;
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder
				.setDateFormat(DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
		gsonBuilder.setExclusionStrategies(new ExclusionStrategy() {
			/**
			 * 设置不要过滤的属性
			 */
			public boolean shouldSkipField(FieldAttributes attr) {
				boolean b = false;
				if (org.apache.commons.lang.StringUtils.isNotEmpty(regStr)) {
					b = regStr.contains("|" + attr.getName() + "|");
				} else {
					b = true;
				}
				return !b;
			}

			/**
			 * 设置要过滤的类
			 */
			public boolean shouldSkipClass(Class<?> clazz) {
				// 这里，如果返回true就表示此类要过滤，否则就输出
				return false;
			}
		});

		Gson gson = gsonBuilder.create();

		// 转换成json
		String objstr = gson.toJson(obj);
		return objstr;
	}

	public static String parseList2Json(List list, String fieldsStr) {
		if (list == null && list.isEmpty())
			return null;

		if (StringUtils.isNotEmpty(fieldsStr)) {
			String returnFileds = "|" + fieldsStr.replaceAll(",", "|") + "|";
			return parseObj2Json(list, returnFileds);
		} else {
			return parseObj2Json(list);
		}
	}

	/**
	 * 将java对象的属性转换成json的key
	 * 
	 * @param obj
	 * @return
	 */
	public static String parseObj2JsonOnField(Object obj) {

		if (null == obj)
			return null;

		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder
				.setDateFormat(DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
		gsonBuilder
				.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES);

		Gson gson = gsonBuilder.create();

		String objstr = gson.toJson(obj);
		return objstr;
	}

	/**
	 * 将json对象转换成java对象
	 * 
	 * @param <T>
	 * @param jsonData
	 * @param c
	 * @return
	 */
	public static <T> Object parseJson2Obj(String jsonData, Class<T> c) {

		if (null == jsonData)
			return null;

		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder
				.setDateFormat(DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
		Gson gson = gsonBuilder.create();

		Object obj = gson.fromJson(jsonData, c);
		return obj;
	}

	/**
	 * 将json对象转换成数组对象
	 * 
	 * @param <T>
	 * @param jsonData
	 * @param c
	 * @return
	 * @throws JSONException
	 */
	public static <T> ArrayList<T> parseJson2List(String jsonData, Class<T> c)
			throws JSONException {

		if (StringUtils.isEmpty(jsonData))
			return null;

		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder
				.setDateFormat(DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
		Gson gson = gsonBuilder.create();

		ArrayList<T> list = new ArrayList<T>();
		JSONArray jsonArray = new JSONArray(jsonData);
		JSONObject objItem = null;
		T objT = null;

		for (int i = 0; i < jsonArray.length(); i++) {
			objItem = (JSONObject) jsonArray.get(i);
			if (null != objItem) {
				objT = gson.fromJson(objItem.toString(), c);
				list.add(objT);
			}
		}
		return list;
	}
	
	public static void main(String args[]){
		ResultObject obj = new ResultObject();
		RegisterUserVO registUserVO = new RegisterUserVO();
		obj.setMsg("");
		obj.setStatus("0");
		obj.setResult(registUserVO);
		System.out.println(JsonMapper.buildNormalMapper().toJson(obj));
		System.out.println(JsonUtils.parseObj2Json(obj));
	}
}